﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WMS.HelperClasses;
using WHMS.HelperClasses;
using WHMS.EntityClasses;

namespace WMS
{
	public partial class UnitConverterForm : Form
	{
		public decimal Value { get; set; }

		public UnitConverterForm(int itemId)
		{
			InitializeComponent();

			LoadData(itemId);
		}

		private void LoadData(int itemId)
		{			
			var unit = Business.GetItemUnitType(itemId);

			UnitToConvertDDL.DataSource = unit.FromUnitConverters;
			UnitToConvertDDL.DisplayMember = "FromUnitName";
			UnitToConvertDDL.ValueMember = "Id";

			ConvertedTypeLbl.Text = unit.Name;
		}

		private void ConvertValue()
		{
			if (QuantityToConvertTB.Text.Length <= 0)
			{
				Value = 0;
			}
			else
			{
				//var converter = (UnitConverterEntity)UnitToConvertDDL.SelectedItem;
				var rate = (UnitToConvertDDL.SelectedItem == null) ? 1 : ((UnitConverterEntity)UnitToConvertDDL.SelectedItem).Rate;
				Value = rate * decimal.Parse(QuantityToConvertTB.Text);
			}

			ConvertedValueLbl.Text = Value.ToString();
		}

		private void OkBtn_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.OK;

			this.Close();
		}

		private void QuantityToConvertTB_TextChanged(object sender, EventArgs e)
		{
			ConvertValue();
		}
		
		private void QuantityToConvertTB_KeyPress(object sender, KeyPressEventArgs e)
		{
			FormsHelper.HandleNumericTextBoxKeypress(sender, e, 5);
		}

		private void UnitToConvertDDL_SelectedIndexChanged(object sender, EventArgs e)
		{
			ConvertValue();
		}
	}
}
